package com.action.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.action.childbase.ChildBaseActivity;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.app_core.View.xrecyclerview.RecyclerAdapter;
import com.base.app_core.View.xrecyclerview.RecyclerViewHolder;
import com.base.app_core.model.Paginated;
import com.base.app_core.model.ShareUser;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.xinglai.yun.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.Collection;
import java.util.List;

import butterknife.BindView;

@Route(path = "/member/myinvite")
public class MyInviteActivity extends ChildBaseActivity<XYBUPressenter> {

    @BindView(R.id.data_container)
    XRecyclerView recyclerView;

    @BindView(R.id.tv_num)
    TextView tvNum;

    private MyInviteAdapter myInviteAdapter;
    private int pageNO = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_invite;
    }

    @Override
    protected void initView() {
        super.initView();
        setToolBar("我邀请的");
        myInviteAdapter = new MyInviteAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(myInviteAdapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageNO = 1;
                if (null != mPresenter) {
                    mPresenter.fetchInviteUser(pageNO);
                }

                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.refreshComplete();
                    }
                }, AppConstants.Const.REFRESHTIME);
            }

            @Override
            public void onLoadMore() {
                pageNO++;
                if (null != mPresenter) {
                    mPresenter.fetchInviteUser(pageNO);
                }

                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (recyclerView != null) {
                            recyclerView.loadMoreComplete();
                        }
                    }
                }, AppConstants.Const.REFRESHTIME);
            }
        });
        recyclerView.setEmptyView(getEmptyView(recyclerView, "没有记录", ""));
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.fetchInviteUser(pageNO);
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
        super.notifyData(object, tClass, paginated);
        if (object instanceof Collection) {
            if (tClass == ShareUser.class) {
                List<ShareUser> o = (List<ShareUser>) object;
                if (XYBAUtils.isEmpty(o)) {
                    tvNum.setVisibility(View.INVISIBLE);
                    return;
                }

                tvNum.setVisibility(View.VISIBLE);
                tvNum.setText("已推广人数:" + paginated.getTotalCount());

                if (myInviteAdapter == null || recyclerView == null) {
                    return;
                }

                if (pageNO == 1) {
                    myInviteAdapter.clear();
                }
                myInviteAdapter.addAll(o);
                myInviteAdapter.notifyDataSetChanged();
                recyclerView.setLoadingMoreEnabled(paginated.getHasMore());
            }
        }
    }

    public static class MyInviteAdapter extends RecyclerAdapter<ShareUser,
            MyInviteActivity.MyInviteAdapter.ViewHolder> {

        @Override
        protected void fillData(ShareUser item, MyInviteActivity.MyInviteAdapter.ViewHolder v) {
            XYBAUtils.displayCircleImage(v.userImage.getContext(), item.getWxIcon(), v.userImage);
            v.recvName.setText(item.getWxNickname());
            v.recvTime.setText(item.getCreateAt());
        }

        @Override
        protected int getItemLayoutId() {
            return R.layout.unit_user_invite;
        }


        public class ViewHolder extends RecyclerViewHolder {
            @BindView(R.id.user_image)
            ImageView userImage;
            @BindView(R.id.recv_name)
            TextView recvName;
            @BindView(R.id.recv_time)
            TextView recvTime;

            public ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

}
